import Vue from 'vue'
import Vuex from 'vuex'
import vuexPer from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list:[]
  },
  getters: {
  },
  mutations: {
    // 添加任务
    add(state,title){
        state.list.push({
            id:new Date().getTime(),
            title,
            status:false,//false为未完成任务
        })
        console.log(state.list);

    },
    del(state,id){
    //    state.list.forEach((item,index)=>{
    //     if(item.id == id){
    //         state.list.splice(index,1)
    //     }
    //    })
    
    // 留下不删除
    state.list = state.list.filter(item=>item.id!=id)
    },
    changeStatus(state,obj){
        let {status,id} = obj
        // state.list[index].status = status
       let index =  state.list.findIndex(item=>{
             return  item.id == id
        })
        state.list[index].status = status
    // state.list.forEach(item=>{
    //     if(item.id == id){
    //         item.status  = status
    //     }
    // })

    }
  },
  actions: {
  },
  modules: {
  },
  plugins:[vuexPer()]
})
